package cn.qn.controller;

import cn.qn.base.BaseQuery;
import cn.qn.base.PageVO;
import cn.qn.common.util.UserSubjectUtil;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.article.AppArticleDTO;
import cn.qn.dto.article.AppArticleQueryDTO;
import cn.qn.dto.article.ArticleAddVo;
import cn.qn.enums.ArticleTypeEnum;
import cn.qn.exceptions.BizException;
import cn.qn.moudle.user.AppUser;
import cn.qn.service.article.ArticleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Bryant
 * @date: 2019/7/18
 * @description: 发现接口
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Reference(check = false,timeout = 30000)
    private ArticleService articleService;

    @RequestMapping(value = "/publish" ,name="发布文章",method = RequestMethod.POST)
    public ResponseDTO publishArticle(@RequestBody ArticleAddVo vo,@RequestHeader String authorization){
        try {
            AppUser appUser = UserSubjectUtil.getAppUser(authorization);
            articleService.appPublishArticle(appUser.getUserId(),vo);
            return ResponseDTO.success();
        }catch (BizException e){
            return ResponseDTO.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/list/attention", name = "我关注的用户的文章", method = RequestMethod.GET)
    public ResponseDTO attentionUserArticles(BaseQuery query,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        AppArticleQueryDTO params = new AppArticleQueryDTO();
        params.setPage(query.getPage());
        params.setSize(query.getSize());
        params.setAttention(true);
        if (null != appUser) {
            params.setUserId(appUser.getUserId());
        }
        PageVO<AppArticleDTO> appUsers = articleService.pageQueryAppArticles(params);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/list/up-to-date", name = "最新文章", method = RequestMethod.GET)
    public ResponseDTO upToDateArticles(BaseQuery query,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        AppArticleQueryDTO params = new AppArticleQueryDTO();
        params.setPage(query.getPage());
        params.setSize(query.getSize());
        params.setAttention(false);
        if (null != appUser) {
            params.setUserId(appUser.getUserId());
        }
        PageVO<AppArticleDTO> appUsers = articleService.pageQueryAppArticles(params);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/list/activity", name = "活动文章", method = RequestMethod.GET)
    public ResponseDTO activityArticles(BaseQuery query,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        AppArticleQueryDTO params = new AppArticleQueryDTO();
        params.setPage(query.getPage());
        params.setSize(query.getSize());
        params.setAttention(false);
        params.setArticleType(ArticleTypeEnum.ACTIVITY.getCode());
        if (null != appUser) {
            params.setUserId(appUser.getUserId());
        }
        PageVO<AppArticleDTO> appUsers = articleService.pageQueryAppArticles(params);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/list/information", name = "活动文章", method = RequestMethod.GET)
    public ResponseDTO informationArticles(BaseQuery query,@RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        AppArticleQueryDTO params = new AppArticleQueryDTO();
        params.setPage(query.getPage());
        params.setSize(query.getSize());
        params.setAttention(false);
        params.setArticleType(ArticleTypeEnum.INFORMATION.getCode());
        if (null != appUser) {
            params.setUserId(appUser.getUserId());
        }
        PageVO<AppArticleDTO> appUsers = articleService.pageQueryAppArticles(params);
        return ResponseDTO.success(appUsers);
    }

    @RequestMapping(value = "/praise/{articleId}", name = "点赞", method = RequestMethod.POST)
    public ResponseDTO praiseArticle(@PathVariable(value = "articleId")String articleId, @RequestHeader String authorization) {
        AppUser appUser = UserSubjectUtil.getAppUser(authorization);
        try {
            articleService.praiseArticle(appUser.getUserId(),articleId);
            return ResponseDTO.success();
        } catch (BizException e) {
            return ResponseDTO.error(e.getMessage());
        }
    }
}
